home *** CD-ROM | disk | FTP | other *** search
/ PCNet 1998 June / PCnet Haziran 1998.iso / Internet / Tools / bs32.exe / _SETUP.1 / ImageMap.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-11-20  |  11.9 KB  |  608 lines

  1. import java.applet.Applet;
  2. import java.awt.Color;
  3. import java.awt.Component;
  4. import java.awt.Event;
  5. import java.awt.Font;
  6. import java.awt.FontMetrics;
  7. import java.awt.Graphics;
  8. import java.awt.Image;
  9. import java.awt.MediaTracker;
  10. import java.awt.Polygon;
  11. import java.awt.Rectangle;
  12. import java.awt.image.ImageObserver;
  13. import java.net.MalformedURLException;
  14. import java.net.URL;
  15. import java.util.StringTokenizer;
  16. import java.util.Vector;
  17.  
  18. public class ImageMap extends Applet implements Runnable {
  19.    Image backgroundImg;
  20.    Image offImage;
  21.    Graphics offGraphic;
  22.    HotArea[] areas;
  23.    HotArea activeArea;
  24.    HotArea downTarget;
  25.    Color bgColor;
  26.    String separator;
  27.    Image enterImage;
  28.    Image downImage;
  29.    String displayString;
  30.    MediaTracker tracker = new MediaTracker(this);
  31.    private int lastMouseX;
  32.    private int lastMouseY;
  33.    private boolean tipOn = false;
  34.    private Thread timerThread;
  35.    private long lastMoveTime;
  36.    private long clearTime;
  37.    private FontMetrics fontInfo;
  38.    Rectangle tipRect;
  39.    Color tipColor;
  40.    Color tipBgColor;
  41.    Font tipFont;
  42.    private long WAIT_TIME;
  43.    private long KEEP_ON_TIME;
  44.    long lastEventTime;
  45.  
  46.    public void init() {
  47.       this.backgroundImg = ((Applet)this).getImage(((Applet)this).getDocumentBase(), ((Applet)this).getParameter("image"));
  48.       this.tracker.addImage(this.backgroundImg, 0);
  49.       String var1 = ((Applet)this).getParameter("bgColor");
  50.       if (var1 == null) {
  51.          this.bgColor = ((Component)this).getBackground();
  52.       } else {
  53.          this.bgColor = this.convertColor(var1);
  54.       }
  55.  
  56.       ((Component)this).setBackground(this.bgColor);
  57.       var1 = ((Applet)this).getParameter("enterImage");
  58.       if (var1 != null && !var1.equals("")) {
  59.          this.enterImage = ((Applet)this).getImage(((Applet)this).getDocumentBase(), var1);
  60.          this.tracker.addImage(this.enterImage, 1);
  61.          Object var3 = null;
  62.       }
  63.  
  64.       var1 = ((Applet)this).getParameter("downImage");
  65.       if (var1 != null && !var1.equals("")) {
  66.          this.downImage = ((Applet)this).getImage(((Applet)this).getDocumentBase(), var1);
  67.          this.tracker.addImage(this.downImage, 1);
  68.          Object var5 = null;
  69.       }
  70.  
  71.       this.tracker.checkID(1, true);
  72.    }
  73.  
  74.    public void start() {
  75.       this.timerThread = new Thread(this);
  76.       this.timerThread.start();
  77.       this.update(((Component)this).getGraphics());
  78.    }
  79.  
  80.    private void delayedInit() {
  81.       ImageEffect var1 = null;
  82.       ImageEffect var2 = null;
  83.       Vector var4 = new Vector();
  84.       HotArea var5 = null;
  85.       this.offImage = ((Component)this).createImage(((Component)this).size().width * 2, ((Component)this).size().height);
  86.       this.offGraphic = this.offImage.getGraphics();
  87.       this.offGraphic.setColor(this.bgColor);
  88.       if (this.backgroundImg != null) {
  89.          this.offGraphic.drawImage(this.backgroundImg, 0, 0, this.bgColor, this);
  90.       }
  91.  
  92.       this.separator = ((Applet)this).getParameter("separator");
  93.       if (this.separator == null) {
  94.          this.separator = "|";
  95.       }
  96.  
  97.       String var3 = ((Applet)this).getParameter("defaultEnterEffect");
  98.       if (var3 != null && !var3.equals("(none)") && !var3.equals("")) {
  99.          try {
  100.             int var6 = var3.indexOf(",");
  101.             String var7;
  102.             if (var6 > 0) {
  103.                var7 = var3.substring(0, var6);
  104.             } else {
  105.                var7 = var3;
  106.             }
  107.  
  108.             var1 = (ImageEffect)Class.forName(var7.toLowerCase().trim()).newInstance();
  109.             if (var6 > 0) {
  110.                var3 = var3.substring(var6 + 1);
  111.             } else {
  112.                var3 = null;
  113.             }
  114.  
  115.             var1.init(this, var3);
  116.          } catch (Exception var17) {
  117.             System.out.println("error processing defaultEnterEffect: " + var3);
  118.          }
  119.       }
  120.  
  121.       var3 = ((Applet)this).getParameter("defaultDownEffect");
  122.       if (var3 != null && !var3.equals("(none)") && !var3.equals("")) {
  123.          try {
  124.             int var33 = var3.indexOf(",");
  125.             String var35;
  126.             if (var33 > 0) {
  127.                var35 = var3.substring(0, var33);
  128.             } else {
  129.                var35 = var3;
  130.             }
  131.  
  132.             var2 = (ImageEffect)Class.forName(var35.toLowerCase()).newInstance();
  133.             if (var33 > 0) {
  134.                var3 = var3.substring(var33 + 1);
  135.             } else {
  136.                var3 = null;
  137.             }
  138.  
  139.             var2.init(this, var3);
  140.          } catch (Exception var16) {
  141.             System.out.println("error processing defaultDownEffect: " + var3);
  142.          }
  143.       }
  144.  
  145.       int var34 = 0;
  146.  
  147.       while(true) {
  148.          while(true) {
  149.             ++var34;
  150.             var3 = ((Applet)this).getParameter("area" + var34);
  151.             if (var3 == null) {
  152.                this.areas = new HotArea[var4.size()];
  153.                var4.copyInto(this.areas);
  154.                var3 = ((Applet)this).getParameter("toolTips");
  155.                if (var3 != null) {
  156.                   StringTokenizer var37 = new StringTokenizer(var3, this.separator);
  157.  
  158.                   while(var37.hasMoreTokens()) {
  159.                      String var39 = var37.nextToken();
  160.  
  161.                      for(int var44 = 0; var44 < this.areas.length; ++var44) {
  162.                         if (this.areas[var44].name != null && this.areas[var44].name.equals(var39)) {
  163.                            if (var37.hasMoreTokens()) {
  164.                               this.areas[var44].tip = var37.nextToken();
  165.                            } else {
  166.                               System.out.println("Error: no tooltip to go with area " + var39);
  167.                            }
  168.                            break;
  169.                         }
  170.                      }
  171.                   }
  172.  
  173.                   var3 = ((Applet)this).getParameter("tipdelay");
  174.                   if (var3 != null) {
  175.                      try {
  176.                         this.WAIT_TIME = (long)Integer.parseInt(var3);
  177.                      } catch (Exception var15) {
  178.                         System.out.println("Could not convert " + var3 + " to an integer, using default value");
  179.                      }
  180.                   }
  181.  
  182.                   this.tipFont = ((Component)this).getGraphics().getFont();
  183.                   String var40 = this.tipFont.getName();
  184.                   int var45 = this.tipFont.getSize();
  185.                   int var49 = this.tipFont.getStyle();
  186.                   var3 = ((Applet)this).getParameter("tipFont");
  187.                   if (var3 != null) {
  188.                      var40 = var3;
  189.                   }
  190.  
  191.                   var3 = ((Applet)this).getParameter("tipSize");
  192.                   if (var3 != null) {
  193.                      try {
  194.                         var45 = Integer.parseInt(var3);
  195.                      } catch (Exception var14) {
  196.                         System.out.println("Could not convert " + var3 + " to an integer, using default value");
  197.                      }
  198.                   }
  199.  
  200.                   var3 = ((Applet)this).getParameter("tipStyle");
  201.                   if (var3 != null) {
  202.                      var3 = var3.toLowerCase();
  203.                      if (var3.equals("plain")) {
  204.                         var49 = 0;
  205.                      } else if (var3.equals("bold")) {
  206.                         var49 = 1;
  207.                      } else if (var3.equals("italic")) {
  208.                         var49 = 2;
  209.                      } else if (var3.equals("bolditalic")) {
  210.                         var49 = 3;
  211.                      } else {
  212.                         System.out.println("Invalid style '" + var3 + "' - using Plain...");
  213.                         var49 = 0;
  214.                      }
  215.                   }
  216.  
  217.                   this.tipFont = new Font(var40, var49, var45);
  218.                   var3 = ((Applet)this).getParameter("tipColor");
  219.                   if (var3 != null) {
  220.                      this.tipColor = this.convertColor(var3);
  221.                   }
  222.  
  223.                   var3 = ((Applet)this).getParameter("tipBgColor");
  224.                   if (var3 != null) {
  225.                      this.tipBgColor = this.convertColor(var3);
  226.                   }
  227.                }
  228.  
  229.                if ((this.tracker.statusAll(true) & 8) != 0) {
  230.                   try {
  231.                      this.tracker.waitForAll();
  232.                   } catch (Exception var13) {
  233.                   }
  234.                }
  235.  
  236.                if (this.tracker.isErrorAny()) {
  237.                   System.out.println("Error loading either or both of the enterImage and downImage");
  238.                }
  239.  
  240.                return;
  241.             }
  242.  
  243.             try {
  244.                StringTokenizer var36 = new StringTokenizer(var3, ",");
  245.                String var8 = var36.nextToken();
  246.                var5 = new HotArea();
  247.                var5.name = var8;
  248.                var5.downEffect = var2;
  249.                var5.enterEffect = var1;
  250.                var8 = var36.nextToken();
  251.                if (!var8.equals("rect") && !var8.equals("circle")) {
  252.                   if (var8.equals("poly")) {
  253.                      var5.type = 3;
  254.  
  255.                      try {
  256.                         Polygon var41 = new Polygon();
  257.                         var5.destination = var36.nextToken();
  258.                         var5.targetFrame = var36.nextToken();
  259.                         if (var5.destination != null && var5.destination.equals("null")) {
  260.                            var5.destination = null;
  261.                         }
  262.  
  263.                         if (var5.targetFrame != null && var5.targetFrame.equals("null")) {
  264.                            var5.targetFrame = null;
  265.                         }
  266.  
  267.                         StringTokenizer var46 = new StringTokenizer(var36.nextToken(), this.separator);
  268.  
  269.                         while(var46.hasMoreTokens()) {
  270.                            int var50 = Integer.parseInt(var46.nextToken());
  271.                            int var51 = Integer.parseInt(var46.nextToken());
  272.                            var41.addPoint(var50, var51);
  273.                         }
  274.  
  275.                         var5.geometry = var41;
  276.                      } catch (Exception var19) {
  277.                         System.out.println("Format error, wrong number or type of parameters for polygon hot area");
  278.                         System.out.println("Ignoring this hot area:");
  279.                         System.out.println(var3);
  280.                         continue;
  281.                      }
  282.                   }
  283.                } else {
  284.                   if (var8.equals("circle")) {
  285.                      var5.type = 2;
  286.                   } else {
  287.                      var5.type = 1;
  288.                   }
  289.  
  290.                   try {
  291.                      var5.destination = var36.nextToken();
  292.                      var5.targetFrame = var36.nextToken();
  293.                      if (var5.destination != null && var5.destination.equals("null")) {
  294.                         var5.destination = null;
  295.                      }
  296.  
  297.                      if (var5.targetFrame != null && var5.targetFrame.equals("null")) {
  298.                         var5.targetFrame = null;
  299.                      }
  300.  
  301.                      int var9 = Integer.parseInt(var36.nextToken());
  302.                      int var10 = Integer.parseInt(var36.nextToken());
  303.                      int var11;
  304.                      int var12;
  305.                      if (var36.countTokens() > 1) {
  306.                         var11 = Integer.parseInt(var36.nextToken()) - var9;
  307.                         var12 = Integer.parseInt(var36.nextToken()) - var10;
  308.                         if (var8.equals("circle")) {
  309.                            var9 -= var11;
  310.                            var10 -= var11;
  311.                            var11 *= 2;
  312.                            var12 = var11;
  313.                         }
  314.                      } else {
  315.                         var11 = Integer.parseInt(var36.nextToken());
  316.                         var12 = var11;
  317.                      }
  318.  
  319.                      var5.geometry = new Rectangle(var9, var10, var11, var12);
  320.                   } catch (Exception var18) {
  321.                      System.out.println("Format error, wrong number or type of parameters for the hot area");
  322.                      System.out.println("Ignoring this hot area:");
  323.                      System.out.println(var3);
  324.                      continue;
  325.                   }
  326.                }
  327.  
  328.                if (var36.hasMoreTokens()) {
  329.                   String var42 = var36.nextToken();
  330.                   if (var42.equals("null")) {
  331.                      var36.nextToken();
  332.                   } else {
  333.                      ImageEffect var47 = (ImageEffect)Class.forName(var42.toLowerCase()).newInstance();
  334.                      var47.init(this, var36.nextToken());
  335.                      var5.enterEffect = var47;
  336.                   }
  337.  
  338.                   if (var36.hasMoreTokens()) {
  339.                      var42 = var36.nextToken();
  340.                      if (var42.equals("null")) {
  341.                         var36.nextToken();
  342.                      } else {
  343.                         ImageEffect var48 = (ImageEffect)Class.forName(var42.toLowerCase()).newInstance();
  344.                         var48.init(this, var36.nextToken());
  345.                         var5.downEffect = var48;
  346.                      }
  347.                      break;
  348.                   }
  349.                }
  350.             } catch (Exception var20) {
  351.                System.out.println("error processing line action" + var34 + ": " + var3);
  352.                break;
  353.             }
  354.          }
  355.  
  356.          var5.theMap = this;
  357.          var4.addElement(var5);
  358.       }
  359.    }
  360.  
  361.    public void setTip(String var1) {
  362.       if (var1 != null) {
  363.          this.displayString = var1;
  364.          this.timerThread.resume();
  365.          if (this.clearTime + this.KEEP_ON_TIME < System.currentTimeMillis()) {
  366.             this.tipOn = false;
  367.          }
  368.  
  369.       }
  370.    }
  371.  
  372.    public void clearTip() {
  373.       if (this.tipRect != null) {
  374.          this.clearTime = System.currentTimeMillis();
  375.          Graphics var1 = this.offImage.getGraphics();
  376.          var1.copyArea(this.tipRect.x + ((Component)this).size().width, this.tipRect.y, this.tipRect.width, this.tipRect.height, -((Component)this).size().width, 0);
  377.          ((Component)this).getGraphics().drawImage(this.offImage, 0, 0, (ImageObserver)null);
  378.          this.tipRect = null;
  379.       } else {
  380.          this.timerThread.suspend();
  381.       }
  382.    }
  383.  
  384.    public void stop() {
  385.       this.timerThread.stop();
  386.    }
  387.  
  388.    public void run() {
  389.       while(true) {
  390.          this.timerThread.suspend();
  391.  
  392.          while(!this.tipOn) {
  393.             long var1 = System.currentTimeMillis();
  394.  
  395.             try {
  396.                Thread.sleep(this.WAIT_TIME);
  397.             } catch (Exception var5) {
  398.             }
  399.  
  400.             if (var1 > this.lastMoveTime) {
  401.                this.tipOn = true;
  402.             }
  403.          }
  404.  
  405.          if (this.displayString != null) {
  406.             Graphics var6 = this.offImage.getGraphics();
  407.             var6.setFont(this.tipFont);
  408.             FontMetrics var2 = var6.getFontMetrics();
  409.             int var3 = var2.stringWidth(this.displayString);
  410.             int var4 = var2.getDescent() + var2.getAscent();
  411.             if (((Component)this).inside(this.lastMouseX, this.lastMouseY + var4 + 25)) {
  412.                if (((Component)this).inside(this.lastMouseX + var3 + 4, this.lastMouseY)) {
  413.                   this.tipRect = new Rectangle(this.lastMouseX, this.lastMouseY + 25, var3 + 4, var4);
  414.                } else {
  415.                   this.tipRect = new Rectangle(this.lastMouseX - var3 - 4, this.lastMouseY + 25, var3 + 4, var4);
  416.                }
  417.             } else if (((Component)this).inside(this.lastMouseX + var3 + 4, this.lastMouseY)) {
  418.                this.tipRect = new Rectangle(this.lastMouseX, this.lastMouseY - var4 - 10, var3 + 4, var4);
  419.             } else {
  420.                this.tipRect = new Rectangle(this.lastMouseX - var3 - 4, this.lastMouseY - var4 - 10, var3 + 4, var4);
  421.             }
  422.  
  423.             var6.copyArea(this.tipRect.x, this.tipRect.y, this.tipRect.width, this.tipRect.height, ((Component)this).size().width, 0);
  424.             var6.setColor(this.tipBgColor);
  425.             var6.fillRect(this.tipRect.x, this.tipRect.y, this.tipRect.width, this.tipRect.height);
  426.             var6.setColor(this.tipColor);
  427.             var6.drawRect(this.tipRect.x, this.tipRect.y, this.tipRect.width - 1, this.tipRect.height - 1);
  428.             var6.drawString(this.displayString, this.tipRect.x + 2, this.tipRect.y + var2.getAscent());
  429.             var6 = ((Component)this).getGraphics();
  430.             var6.clipRect(this.tipRect.x, this.tipRect.y, this.tipRect.width, this.tipRect.height);
  431.             var6.drawImage(this.offImage, 0, 0, (ImageObserver)null);
  432.          }
  433.       }
  434.    }
  435.  
  436.    public boolean handleEvent(Event var1) {
  437.       if (var1.when >= this.lastEventTime) {
  438.          this.lastEventTime = var1.when;
  439.          return super.handleEvent(var1);
  440.       } else {
  441.          return true;
  442.       }
  443.    }
  444.  
  445.    public boolean mouseMove(Event var1, int var2, int var3) {
  446.       if (this.areas == null) {
  447.          return true;
  448.       } else if (this.lastMouseX == var2 && this.lastMouseY == var3) {
  449.          return true;
  450.       } else {
  451.          this.lastMouseX = var2;
  452.          this.lastMouseY = var3;
  453.          this.lastMoveTime = var1.when;
  454.  
  455.          for(int var4 = 0; var4 < this.areas.length; ++var4) {
  456.             if (this.areas[var4].inside(var2, var3)) {
  457.                this.areas[var4].checkEnter();
  458.             } else {
  459.                this.areas[var4].checkExit();
  460.             }
  461.          }
  462.  
  463.          return true;
  464.       }
  465.    }
  466.  
  467.    public boolean mouseDrag(Event var1, int var2, int var3) {
  468.       return this.mouseMove(var1, var2, var3);
  469.    }
  470.  
  471.    public boolean mouseExit(Event var1, int var2, int var3) {
  472.       if (this.areas == null) {
  473.          return true;
  474.       } else {
  475.          for(int var4 = 0; var4 < this.areas.length; ++var4) {
  476.             if (this.downTarget == this.areas[var4] && this.downTarget.downEffect != null) {
  477.                this.downTarget.downEffect.stop(this.downTarget);
  478.             }
  479.  
  480.             this.areas[var4].checkExit();
  481.          }
  482.  
  483.          return true;
  484.       }
  485.    }
  486.  
  487.    public boolean mouseDown(Event var1, int var2, int var3) {
  488.       if (this.activeArea != null && this.activeArea.mouseInside) {
  489.          this.clearTip();
  490.          this.downTarget = this.activeArea;
  491.          if (this.downTarget.downEffect != null) {
  492.             this.downTarget.downEffect.start(this.downTarget, this.offImage.getGraphics());
  493.             ((Component)this).getGraphics().drawImage(this.offImage, 0, 0, (ImageObserver)null);
  494.          }
  495.  
  496.          return true;
  497.       } else {
  498.          return true;
  499.       }
  500.    }
  501.  
  502.    public boolean mouseUp(Event var1, int var2, int var3) {
  503.       if (this.downTarget == this.activeArea && this.activeArea != null) {
  504.          if (this.downTarget.downEffect != null) {
  505.             this.downTarget.downEffect.stop(this.downTarget);
  506.          }
  507.  
  508.          if (this.downTarget.destination == null) {
  509.             this.downTarget = null;
  510.             return true;
  511.          } else {
  512.             try {
  513.                if (this.downTarget.targetFrame == null) {
  514.                   ((Applet)this).getAppletContext().showDocument(new URL(((Applet)this).getDocumentBase(), this.downTarget.destination));
  515.                } else {
  516.                   ((Applet)this).getAppletContext().showDocument(new URL(((Applet)this).getDocumentBase(), this.downTarget.destination), this.downTarget.targetFrame);
  517.                }
  518.             } catch (MalformedURLException var4) {
  519.                System.out.println("The specified URL is not formatted correctly: " + this.downTarget.destination);
  520.             }
  521.  
  522.             this.downTarget = null;
  523.             return true;
  524.          }
  525.       } else {
  526.          this.downTarget = null;
  527.          return true;
  528.       }
  529.    }
  530.  
  531.    public void update(Graphics var1) {
  532.       if (this.offImage != null) {
  533.          var1.drawImage(this.offImage, 0, 0, this);
  534.       } else {
  535.          var1.drawImage(this.backgroundImg, 0, 0, (ImageObserver)null);
  536.          if (this.tracker.checkID(0, true)) {
  537.             this.delayedInit();
  538.             ((Component)this).repaint();
  539.          } else {
  540.             ((Component)this).repaint(500L);
  541.          }
  542.       }
  543.    }
  544.  
  545.    public void paint(Graphics var1) {
  546.       this.update(var1);
  547.    }
  548.  
  549.    protected Color convertColor(String var1) {
  550.       if (var1 == null) {
  551.          return Color.black;
  552.       } else {
  553.          var1 = var1.toLowerCase().trim();
  554.          if (var1.equals("black")) {
  555.             return Color.black;
  556.          } else if (var1.equals("blue")) {
  557.             return Color.blue;
  558.          } else if (var1.equals("cyan")) {
  559.             return Color.cyan;
  560.          } else if (var1.equals("darkgray")) {
  561.             return Color.darkGray;
  562.          } else if (var1.equals("gray")) {
  563.             return Color.gray;
  564.          } else if (var1.equals("green")) {
  565.             return Color.green;
  566.          } else if (var1.equals("lightgray")) {
  567.             return Color.lightGray;
  568.          } else if (var1.equals("magenta")) {
  569.             return Color.magenta;
  570.          } else if (var1.equals("orange")) {
  571.             return Color.orange;
  572.          } else if (var1.equals("pink")) {
  573.             return Color.pink;
  574.          } else if (var1.equals("red")) {
  575.             return Color.red;
  576.          } else if (var1.equals("white")) {
  577.             return Color.white;
  578.          } else if (var1.equals("yellow")) {
  579.             return Color.yellow;
  580.          } else {
  581.             try {
  582.                return new Color(Integer.parseInt(var1, 16));
  583.             } catch (NumberFormatException var4) {
  584.                String var2 = var1.substring(1);
  585.  
  586.                try {
  587.                   return new Color(Integer.parseInt(var2, 16));
  588.                } catch (NumberFormatException var3) {
  589.                   System.out.println("Could not parse: '" + var1 + "' into a color, using black");
  590.                   return Color.black;
  591.                }
  592.             }
  593.          }
  594.       }
  595.    }
  596.  
  597.    public String getAppletInfo() {
  598.       return "Imagemap 1.0 - Copyright ┬⌐ 1996, Macromedia, Inc.";
  599.    }
  600.  
  601.    public ImageMap() {
  602.       this.tipColor = Color.black;
  603.       this.tipBgColor = Color.white;
  604.       this.WAIT_TIME = 250L;
  605.       this.KEEP_ON_TIME = 250L;
  606.    }
  607. }
  608.